#ifndef DATATREEMODEL_H
#define DATATREEMODEL_H


#include "model_global.h"
#include "abstractmodelitem.h"


#include <QAbstractItemModel>
#include <QPointer>


class MODELSHARED_EXPORT ItemTreeModel : public QAbstractItemModel
{
    Q_OBJECT

public:

    ItemTreeModel(QObject *parent = 0);
    ItemTreeModel(AbstractModelItem* root, QObject* parent = 0);

    void setRoot(AbstractModelItem* root);
    AbstractModelItem* root() const;

    template<typename T>
    T root() const
    {
        return qobject_cast<T>(root());
    }



    virtual QModelIndex index(int row, int column, const QModelIndex &parent) const;
    virtual QModelIndex parent(const QModelIndex &child) const;

    virtual int rowCount(const QModelIndex &parent) const;
    virtual int columnCount(const QModelIndex &parent) const;

    virtual QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
    virtual bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);

    virtual QVariant headerData(int section, Qt::Orientation orientation, int role) const;


    virtual bool insertRows(int type, int count, const QModelIndex &parent);
    virtual bool removeRows(int row, int count, const QModelIndex &parent);


    AbstractModelItem* itemFromIndex(const QModelIndex& index) const;
    template <typename T>
    T itemFromIndex(const QModelIndex &index) const
    {
        return qobject_cast<T>(itemFromIndex(index));
    }


public slots:

    virtual void refreshHeader(const QModelIndex &headerSource);

    virtual void onFieldCountChanged();


protected:

    QPointer<AbstractModelItem> m_root;

    QPersistentModelIndex m_headerSource;

};



#endif // DATATREEMODEL_H


